package com.hfkh.dao.impl;

import com.hfkh.dao.UserDao;
import com.hfkh.util.BaseDAO;
import com.hfkh.vo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserDaoImpl extends BaseDAO implements UserDao {
    @Override
    public List<User> findAll() {
        List<User> userList = new ArrayList<>();
        User user = null;
        String sql = "select userid,username,password,name,email,mobile from users";
        List<Map<String,Object>> mapList = executeQuery(sql,null);
        for (Map<String,Object> tempMap:mapList) {
            String userId = (String)tempMap.get("userid");
            String userName = (String)tempMap.get("username");
            String password = (String)tempMap.get("password");
            String name = (String)tempMap.get("name");
            String email = (String)tempMap.get("email");
            String mobile = (String)tempMap.get("mobile");
            user = new User(userId,userName,password,name,email,mobile);
            userList.add(user);
        }
        return userList;
    }

    @Override
    public User findById(String userId) {//根据id查找
        User targetUser = null;
        List<User> userList = findAll();//调用findAll获取所有的用户，再遍历
        for (User user:userList) {
           String targetId = user.getUserId();
           if (targetId.equals(userId)){
               targetUser = user;
               break;
           }
        }
        return targetUser;
    }

    @Override
    public boolean add(User user) {
        boolean isAdd = false;
        int rows = 0;
        String userId = user.getUserId();
        String userName = user.getUserName();
        String password = user.getPassword();
        String name = user.getName();
        String email = user.getEmail();
        String mobile = user.getMobile();
        Object[] colValue = new Object[]{userId,userName,password,name,email,mobile};
        String sql = "insert into users (userId,userName,password,name,email,mobile)values(?,?,?,?,?,?)";
        rows = executeUpdate(sql,colValue);
        if (rows > 0){
            isAdd = true;
        }
        return isAdd;
    }

    @Override
    public boolean delete(String userId) {
        boolean isDelete = false;
        int rows = 0;
        String sql = "delete from users where userId = ?";
        Object[] colValue = new Object[]{userId};
        rows = executeUpdate(sql,colValue);
        if (rows > 0){
            isDelete = true;
        }
        return isDelete;
    }

    @Override
    public boolean change(User user) {
        //当因为我们在其他的java类中改变了这个user对象后，那么相应的也要改变它在数据库中的信息
        boolean isChange = false;
        int rows = 0;
        String sql = "update users set userId = ?,userName = ?,password = ?,name = ?,email = ?,mobile = ?where userId = ?";
        rows = executeUpdate(sql,new Object[]{user.getUserId(),user.getUserName(),user.getPassword(),user.getName(),user.getEmail(),user.getMobile(),user.getUserId()});
        if (rows > 0){
            isChange = true;
        }
        return isChange;
    }
}
